/**
 * 
 */
package com.bestbuy.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.regexp.REUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bestbuy.pojo.Cart;
import com.bestbuy.pojo.CartDetail;
import com.bestbuy.pojo.Catalog;
import com.bestbuy.pojo.Product;
import com.bestbuy.service.CartService;
import com.bestbuy.service.CatalogService;

/**
 * @author Tuan
 * 
 */
@Controller
public class CartController {

	@Autowired
	private CatalogService catalogService;

	@Autowired
	private CartService cartService;

	@RequestMapping(value = { "/addtocart.do" }, method = RequestMethod.GET)
	public String AddToCart(
			@RequestParam(value = "tbidProduct") String idProduct,
			@RequestParam(value = "tbQuantity") String quantity, Model model,
			HttpServletRequest request) 
	{
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();

		String name = auth.getName();
		if (name.equals("anonymousUser")) 
		{
			// kiem tra co phai danng nhap bang facebook ko
			String temp = (String)request.getSession().getAttribute("username");
			if (temp == null)
			{
				//List<Catalog> listCatalog = catalogService.getListCatalog();
				List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
				model.addAttribute("catalogs", listCatalog);

				model.addAttribute("Page", "AccountLogin");
				return "Main";
			}
			else
			{
				cartService.addToCart(temp, Integer.parseInt(idProduct),
						Integer.parseInt(quantity));
				Cart cart = cartService.getCart(temp);
				List<CartDetail> cartDetail = cartService.getCartDetail(cart
						.getIdcart());
				model.addAttribute("cartDetail", cartDetail);
				Float tong = 0f;
				for (int i = 0; i < cartDetail.size(); i++) {
					if (cartDetail.get(i).getProduct().getPromotion().getDiscount() != 0.0f) {
						tong += (cartDetail.get(i).getProduct().getPrice() * cartDetail
								.get(i).getQuantity() * (100 - cartDetail.get(i).getProduct().getPromotion().getDiscount()) /100);
					}
					else
					{
						tong += (cartDetail.get(i).getProduct().getPrice() * cartDetail
								.get(i).getQuantity());
					}
				}
				cart = cartService.getCart(temp);
				cart.setTotal(tong);
				cartService.updateCart(cart);
				model.addAttribute("TongTien", tong);
				//List<Catalog> listCatalog = catalogService.getListCatalog();
				List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
				model.addAttribute("catalogs", listCatalog);
				model.addAttribute("Page", "Cart");
				return "Main";
			}
			
		}
		else 
		{
			cartService.addToCart(name, Integer.parseInt(idProduct),
					Integer.parseInt(quantity));
			Cart cart = cartService.getCart(name);
			List<CartDetail> cartDetail = cartService.getCartDetail(cart
					.getIdcart());
			model.addAttribute("cartDetail", cartDetail);
			Float tong = 0f;
			for (int i = 0; i < cartDetail.size(); i++) {
				if (cartDetail.get(i).getProduct().getPromotion().getDiscount() != 0.0f) {
					tong += (cartDetail.get(i).getProduct().getPrice() * cartDetail
							.get(i).getQuantity() * (100 - cartDetail.get(i).getProduct().getPromotion().getDiscount()) /100);
				}
				else
				{
					tong += (cartDetail.get(i).getProduct().getPrice() * cartDetail
							.get(i).getQuantity());
				}
			}
			cart = cartService.getCart(name);
			cart.setTotal(tong);
			cartService.updateCart(cart);
			model.addAttribute("TongTien", tong);
			//List<Catalog> listCatalog = catalogService.getListCatalog();
			List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
			model.addAttribute("catalogs", listCatalog);
			model.addAttribute("Page", "Cart");
			return "Main";
		}

	}

	@RequestMapping(value = { "/removecart.do" }, method = RequestMethod.GET)
	public String RemoveCart(
			@RequestParam(value = "idcartdetail") String idCartDetail,
			Model model) {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		String name = auth.getName();
		cartService.removeProduct(idCartDetail);
		Cart cart = cartService.getCart(name);
		List<CartDetail> cartDetail = cartService.getCartDetail(cart
				.getIdcart());
		model.addAttribute("cartDetail", cartDetail);
		Float tong = 0f;

		for (int i = 0; i < cartDetail.size(); i++) {
			if (cartDetail.get(i).getProduct().getPromotion().getDiscount() != 0.0f) {
				tong += (cartDetail.get(i).getProduct().getPrice() * cartDetail
						.get(i).getQuantity() * (100 - cartDetail.get(i).getProduct().getPromotion().getDiscount()) /100);
			} else {
				tong += (cartDetail.get(i).getProduct().getPrice() * cartDetail
						.get(i).getQuantity());
			}
		}
		cart = cartService.getCart(name);
		cart.setTotal(tong);
		cartService.updateCart(cart);
		model.addAttribute("TongTien", tong);
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		model.addAttribute("Page", "Cart");
		return "Main";
	}

	@RequestMapping(value = { "/cart.do" }, method = RequestMethod.GET)
	public String RemoveCart(Model model) {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		String name = auth.getName();
		if (name.equals("anonymousUser")) {
			//List<Catalog> listCatalog = catalogService.getListCatalog();
			List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
			model.addAttribute("catalogs", listCatalog);

			model.addAttribute("Page", "AccountLogin");
			return "Main";
		} else {
			Cart cart = cartService.getCart(name);
			List<CartDetail> cartDetail = cartService.getCartDetail(cart
					.getIdcart());
			model.addAttribute("cartDetail", cartDetail);
			Float tong = 0f;
			for (int i = 0; i < cartDetail.size(); i++) {
				tong += (cartDetail.get(i).getProduct().getPrice() * cartDetail
						.get(i).getQuantity());
			}
			cart = cartService.getCart(name);
			cart.setTotal(tong);
			cartService.updateCart(cart);
			model.addAttribute("TongTien", tong);
			//List<Catalog> listCatalog = catalogService.getListCatalog();
			List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
			model.addAttribute("catalogs", listCatalog);
			model.addAttribute("Page", "Cart");
			return "Main";
		}
	}
	@RequestMapping(value = { "/updatecart.do" }, method = RequestMethod.GET)
	public String UpdateCart(
			@RequestParam(value = "tbquality") String quanlity,
			@RequestParam(value = "tbidcartdetail") String idCartDetail,
			Model model) {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		String name = auth.getName();
		cartService.updateProduct(idCartDetail, quanlity);
		Cart cart = cartService.getCart(name);
		List<CartDetail> cartDetail = cartService.getCartDetail(cart
				.getIdcart());
		model.addAttribute("cartDetail", cartDetail);
		Float tong = 0f;
		for (int i = 0; i < cartDetail.size(); i++) {
			if (cartDetail.get(i).getProduct().getPromotion().getDiscount() != 0.0f) {
				tong += (cartDetail.get(i).getProduct().getPrice() * cartDetail
						.get(i).getQuantity() * (100 - cartDetail.get(i).getProduct().getPromotion().getDiscount()) /100);
			} else {
				tong += (cartDetail.get(i).getProduct().getPrice() * cartDetail
						.get(i).getQuantity());
			}
		}
		cart = cartService.getCart(name);
		cart.setTotal(tong);
		cartService.updateCart(cart);
		model.addAttribute("TongTien", tong);
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		model.addAttribute("Page", "Cart");
		return "Main";
	}

	@RequestMapping(value = { "/checkout.do" }, method = RequestMethod.GET)
	public String ThanhToan(Model model, HttpServletRequest request) {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		// kiem tra co phai danng nhap bang facebook ko
		String temp = (String)request.getSession().getAttribute("username");
		String name= null;
		if (temp != null)
		{
			name = temp;
		}
		else
		{
			name = auth.getName();
		}
		
		cartService.saveBill(name);
		//List<Catalog> listCatalog = catalogService.getListCatalog();
		List<Catalog> listCatalog = catalogService.getListCatalogByDeleted("0");
		model.addAttribute("catalogs", listCatalog);
		model.addAttribute("Page", "Bill");
		return "Main";
	}
}
